﻿using System;
using System.Web;
using System.Collections.Generic;

using SiteUnity.Layers.Core.Provider;
using SiteUnity.Layers.Entity.General;
using SiteUnity.Frame.Common.Tools.Class;
using SiteUnity.Frame.Common.Tools.Data;

namespace SiteUnity.Layers.Application
{
    public class SiteManager
    {
        static private GeneralProvider _generalprovider = new GeneralProvider();

        static public bool isPostBack { get; set; }

        static public Website Website
        {
            get
            {
                Website website = new Website();

                Cache.AddIfNotExists("Website", out website, _generalprovider, "selectWebsite", new[] { website });
                   
                website.RawUrl = HttpContext.Current.Request.RawUrl;

                return website;
            }
        }

        static public List<PageType> PageTypes
        {
            get
            {
                List<PageType> pagetypes;

                Cache.AddIfNotExists("PageTypes", out pagetypes, _generalprovider, "selectPageType", null);

                return pagetypes;
            }
        }

        static public List<WebsiteSetting> WebsiteSetting
        {
            get
            {
                List<WebsiteSetting> websiteSetting;

                Cache.AddIfNotExists("WebsiteSetting", out websiteSetting, _generalprovider, "selectWebsiteSettingAll", new[] { Website });

                return websiteSetting;
            }
        }

        static public List<Page> selectRecentPage(int count)
        {
            List<Page> tmpPages = _generalprovider.selectRecentPage(Website, count);
            List<Page> result = new List<Page>();

            foreach (Page page in tmpPages)
            {
                result.Add(Website.SelectPageByUrl(page.Url));
            }

            return result;
        }

        static public PageType FindPageTypeByID(int ID)
        {
            PageType type= ClassCollection.Find(SiteManager.PageTypes, "ID", ID);
            
            return type;
        }

        static public WebsiteSetting FindWebsiteSettingByKey(string key)
        {
            WebsiteSetting setting= ClassCollection.Find(SiteManager.WebsiteSetting, "Key", key);

            return setting;
        }
    }
}
